var res = {
    startGame_png: "res/startGame.png",
    startGameUnable_jpg: "res/startGameUnable.jpg",
    loadGame_png: "res/loadGame.png",
    loadGameUnable_jpg: "res/loadGameUnable.jpg",
    monster_png: "res/monster.png",
    daohen_png: "res/daohen.png",
    quanhen_png: "res/quanhen.png",
    up_png: "res/up.png",
    upSelected_png: "res/up2.png",
    down_png: "res/down.png",
    downSelected_png: "res/down2.png",
    autoAck_png: "res/autoAck.png",
    autoAck2_png: "res/autoAck2.png",
    property_png: "res/property.png",
    shop_png: "res/shop.png",
    skill_png: "res/skill.png",
    return_png:"res/return.png",
    right_png:"res/right.png",
    wrong_png:"res/wrong.png",

    heroAtt_mp3: "res/music/heroAtt.mp3",
    enemyAtt_mp3: "res/music/enemyAtt.mp3",
    dead_wav: "res/music/dead.wav",
    dadada_mp3:"res/music/dadada.mp3",

    //cartoonMusic_mp3:"res/music/cartoonMusic.mp3",

    cartoonMusic_mp3:"res/music/cartoonMusic2.mp3",
    gameMusic_mp3:"res/music/gameMusic.mp3",
    gameBegin_mp3:"res/music/ChillMusic.mp3",
    gameMenu_mp3:"res/music/gameMenu.mp3",

    gameOver_wav: "res/music/gameOver.wav",
    leaveUp_mp3: "res/music/leaveUp.mp3",
    //desire_drivers_mp3:"res/music/desire_drivers.mp3",


    pause_png: "res/pause.png",
    start_png: "res/start.png",
    cartoon_png: "res/cartoon.png",
    //cartoon_json:"res/cartoon.json"

    Image1_jpg: "res/bg/Image1.jpg",
    shopBg_jpg:"res/bg/shop.jpg",
    wuqi_gif:"res/equipment/sword/1.gif",

    fonts:{
        type:"font",
        name:"HanWangShinSuMedium",
        srcs:["res/font/my.ttf"]
    }

};

var g_resources = [];
for (var i in res) {
    g_resources.push(res[i]);
}

var size = cc.winSize;







//global function
var gf = {

    //obj translate to json,if true, translate  hero,false translate enemy
    getObjJson: function (objJson, isHero) {
        //console.log(objJson);
        if (isHero) {
            var currentHero = g.storage.getItem("currentHero");
            var heroJson = {};
            heroJson.type = objJson.type;
            heroJson.mySprite = heroArray[currentHero]["mySprite"];//**
            heroJson.name = objJson.name;
            heroJson.hp = objJson.hpMax; //**
            heroJson.hpMax = objJson.hpMax;
            heroJson.level = objJson.level;
            heroJson.exp = objJson.exp;
            heroJson.expArr = objJson.expArr;
            heroJson.attackNumber = objJson.attackNumber;
            heroJson.defenseNumber = objJson.defenseNumber;
            heroJson.critical = objJson.critical;
            heroJson.dodge = objJson.dodge;
            heroJson.fiveElement = objJson.fiveElement;

            heroJson.power = objJson.power;
            heroJson.health = objJson.health;
            heroJson.brains = objJson.brains;
            heroJson.agile = objJson.agile;
            heroJson.powerGrow = objJson.powerGrow;
            heroJson.healthGrow = objJson.healthGrow;
            heroJson.brainsGrow = objJson.brainsGrow;
            heroJson.agileGrow = objJson.agileGrow;

            var heroText = JSON.stringify(heroJson);
            return heroText;
        } else {
            var enemyJson = {};
            var currentEnemy = g.storage.getItem("currentEnemy");
            enemyJson.type = objJson.type;
            enemyJson.mySprite = enemyArray[currentEnemy]["mySprite"];
            enemyJson.name = objJson.name;
            enemyJson.hp = objJson.hpMax;
            enemyJson.hpMax = objJson.hpMax;
            enemyJson.level = objJson.level;
            enemyJson.exp = objJson.exp;
            enemyJson.attackNumber = objJson.attackNumber;
            enemyJson.defenseNumber = objJson.defenseNumber;
            enemyJson.critical = objJson.critical;
            enemyJson.dodge = objJson.dodge;
            enemyJson.fiveElement = objJson.fiveElement;
            var enemyText = JSON.stringify(enemyJson);
            return enemyText;
        }
    },
    //to tranlate json between scenes
    GlobleSave: function (json) {
        var heroText = this.getObjJson(json, true);
        g.storage.setItem("hero", heroText);
    },
    //parameter is the array
    initEnemy: function (enemyArray,isHero) {
        for (var i = 0; i < enemyArray.length; i++) {
            if(isHero){
                var level = enemyArray[i].level+2;
            }else{
                var level = enemyArray[i].level;
                enemyArray[i].exp = 10 +  level * level * 10;
            }

            enemyArray[i].power = 18 + enemyArray[i].powerGrow * level;
            enemyArray[i].health = 18 + enemyArray[i].healthGrow * level;
            enemyArray[i].brains = 18 + enemyArray[i].brainsGrow * level;
            enemyArray[i].agile = 18 + enemyArray[i].agileGrow * level;
            enemyArray[i].hpMax = enemyArray[i].health * 5;
            enemyArray[i].hp = enemyArray[i].hpMax;
            enemyArray[i].defenseNumber = enemyArray[i].health;
            enemyArray[i].attackNumber = enemyArray[i].power * 3;
            enemyArray[i].critical = enemyArray[i].agile * 0.004;
            enemyArray[i].dodge = enemyArray[i].agile * 0.002;
            enemyArray[i].weapon = null;
            enemyArray[i].protectionSuit = null;
            enemyArray[i].shoe = null;
        }
    },
    //
    gradualShow:function(layer,wholeStr,isRightRemove,fn,attr){

        cc.audioEngine.playMusic(res.dadada_mp3,true);

        var str1 = new cc.LabelTTF("", "Arial", 38,cc.size(size.width-size.width/4, size.height/2));
        str1.x = size.width / 2;
        str1.y = size.height / 4;
        if(attr){
            str1.x = attr.x;
            str1.y = attr.y;
        }
        layer.addChild(str1);
        var i=2;
        function type(){
            if(i>=wholeStr.length){
                //next str
                window.clearInterval(Myupdate);
                cc.audioEngine.stopMusic(res.dadada_mp3);
                if(isRightRemove){
                    //delay remove
                        layer.removeChild(str1);
                }

                if(fn){
                    fn();
                }
            }
            i=i+1;
            var str = wholeStr.substring(0,i);
            str1.initWithString(str,"Arial", 38,cc.size(size.width-size.width/4, size.height/2));
            //str1.initWithString(str,"HanWangShinSuMedium", 38,cc.size(size.width-size.width/4, size.height/2));
        }
        var Myupdate =  setInterval(type, 100);
    },
    //addBtn:function(layer,res,attr,fn){
        //var changeNameBtn = new ccui.Button(res.down_png, res.downSelected_png);
        //changeNameBtn.attr({
        //    x: size.width / 5,
        //    y: size.height / 2,
        //});
        //changeNameBtn.addClickEventListener(this.changeName);
        //this.addChild(changeNameBtn);
    //}


};


gf.initEnemy(enemyArray);
gf.initEnemy(heroArray,true);